<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{User};
use think\Db;
use think\facade\Log;

class UserLevel extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('UserLevel')->setDescription('会员到期检测');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        (new User())->where(['level' => [2]])->where('level_time', '<', date('Y-m-d H:i:s'))->chunk(10, function ($lists) {
            foreach ($lists as $list) {
                (new User())->where(['id' => $list['id'], 'level' => $list['level']])->update(['level' => 1]);
            }
        });
    }
}
